<template>
  <div>
    <el-button @click="contentInfoAdd" type="primary" v-if="contentData.name">添加{{contentData.name}}</el-button>
    <el-table :data="contentInfo" style="width: 100%">
      <el-table-column show-overflow-tooltip prop="pid" label="PID" width="180"> </el-table-column>
      <el-table-column show-overflow-tooltip prop="name" label="名称" width="180"> </el-table-column>
      <el-table-column show-overflow-tooltip prop="url" label="访问地址"> </el-table-column>
      <el-table-column show-overflow-tooltip prop="image" label="图片地址"> </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 弹窗 -->
    <el-dialog title="添加数据" :visible.sync="dialogInfoVisible" width="30%" :before-close="handleClose">
      <!-- 表单 -->
      <el-form :model="ruleForm" status-icon ref="ruleForm" label-width="100px">
        <el-form-item label="名称" prop="name">
          <el-input type="text" v-model="ruleForm.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="访问地址" prop="url">
          <el-input type="text" v-model="ruleForm.url" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="图片地址" prop="image">
          <el-input type="text" v-model="ruleForm.image" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogInfoVisible = false">取 消</el-button>
        <el-button type="primary" @click="contentInfoAddHandle">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { mapState, mapActions } from "vuex";
import { contentInfoAdd, contentInfoDel } from '@/api'
export default {
  data() {
    return {
      dialogInfoVisible: false,
      ruleForm:{
        name: '',
        url: '',
        image: ''
      }
    };
  },
  computed: {
    ...mapState("contentInfoModule", ["contentInfo","contentData"]),
  },
  methods:{
    ...mapActions("contentInfoModule",["asyncSetContentInfo"]),
    contentInfoAdd(){
      this.dialogInfoVisible = true;
    },
    async contentInfoAddHandle(){
      try {
        const res = await contentInfoAdd({
          pid: this.contentData.pid,
          name: this.ruleForm.name,
          url: this.ruleForm.url,
          image: this.ruleForm.image
        });
        // console.log(res);
        if(res.status === 200){
          //关闭弹窗
          this.dialogInfoVisible = false;
          //重新刷新
          this.asyncSetContentInfo(this.contentData);
          this.$message({
            message: '添加成功！',
            type: 'success'
          });
          //清空表单
          this.ruleForm = {
            name: '',
            url: '',
            image: ''
          }
        }else{
          this.$message({
            message: '添加失败...',
            type: 'error'
          });
        }
      } catch (error) {
        console.log(error);
      }
    },
    handleClose(){
      this.dialogInfoVisible = false;
    },
    //删除
    handleDelete(index, row){
      this.$confirm('你确定要删除内容吗？', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(async ()=> {
        try {
          const res = await contentInfoDel({id: row.id})
          // console.log(res);
          if(res.status === 200){
            this.asyncSetContentInfo(this.contentData);
            this.$message({
              message: '删除成功！',
              type: 'success'
            });
          }
        } catch (error) {
          console.log(error);
        }
      }).catch(() => {
        console.log('取消删除');
        this.$message({
          message: '已取消删除！',
          type: 'info'
        })
      })
    }
  }
};
</script>

<style lang="less" scoped>
  div > button{
    margin-bottom: 10px;
    float: right;
    right: 0;
    margin-right: 42%;
  }
</style>